<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/9/12
 * Time: 23:34
 */

namespace Api\Domain;

use Api\Domain\BaseDomain;
use Api\Service\WeixinService;
use Common\Api\AccountApi;
use Common\ConstVar\BoyeActionConstModel;
use Shop\Api\MemberConfigApi;
use Uclient\Model\OAuth2TypeModel;
use Uclient\Model\UcenterMemberModel;

class WeixinDomain extends BaseDomain{

    private $appid;
    private $appsecret;

    public function __construct(){
        parent::__construct();
        $this -> _init();
    }

    public function _init(){
        //微信开放应用的appid和appsecret
        $this->appid = "wxa30658c4f4f6bbf4";
        $this->appsecret = "69c4d761d4a0801bba64bef359bbf0b2";
    }

    /**
     * 微信绑定
     */
    public function bind(){

        $code =  $this->_post('code','','缺少code参数');
        $uid = $this->_post('uid','','缺少uid参数');
        addLog("Weixin/bind",$_GET,$_POST,"调用绑定接口CODE=".$code);

        $service = new WeixinService($this->appid,$this->appsecret);

        $result = $service->getUserInfo($code);

        if(!$result['status']){
            $this->apiReturnErr($result['info']);
        }
        $weixin_userinfo = $result['info'];

        if(empty($weixin_userinfo)){
            $this->error("微信用户信息获取失败!");
        }

        $openid = $weixin_userinfo['openid'];

        $result = apiCall(AccountApi::GET_INFO_BY_WXOPENID,array($openid));



        //如果已经存在，则返回信息
        if($result['status'] && is_array($result['info'])){
            if($result['info']['uid'] == $uid){
                $this->apiReturnSuc("绑定成功!");
            }

            $this->apiReturnErr("该微信已经绑定另一个账户!");
        }

        $entity = array(
            'wxopenid'=>$openid,
            'weixin_bind'=>1,
        );

        $result = apiCall(MemberConfigApi::SAVE_BY_ID,array($uid,$entity));

        if(!$result['status']){
            $this->apiReturnErr($result['info']);
        }else{
            $this -> apiReturnSuc("绑定成功!");
        }


    }

    /**
     * 微信注册
     */
    public function login(){

        $code =  $this->_post('code','','缺少code参数');

        addLog("Weixin/login",$_GET,$_POST,"调用微信登录接口CODE=".$code);

        $service = new WeixinService($this->appid,$this->appsecret);

        $result = $service->getUserInfo($code);

        if(!$result['status']){
            $this->apiReturnErr($result['info']);
        }

        $weixin_userinfo = $result['info'];

        addLog("Weixin/login",$weixin_userinfo,$weixin_userinfo,"微信登录拉取的用户信息");
        //注册用户账户

        $openid = $weixin_userinfo['openid'];
        $password = $openid;
        $nickname = $weixin_userinfo['nickname'];
        $sex = $weixin_userinfo['sex'];
        $city = $weixin_userinfo['city'];
        $province = $weixin_userinfo['province'];
        $country = $weixin_userinfo['country'];
        $head = $weixin_userinfo['headimgurl'];
        $idcode = getIDCode(rand(10000000000,99999999999),'X');

        $result = apiCall(AccountApi::GET_INFO_BY_WXOPENID,array($openid));

        addLog("Weixin/login",$result,$result,"微信登录从数据库获取的用户信息".$openid);
        //如果已经存在，则返回信息
        if($result['status'] && is_array($result['info'])){
            $this->apiReturnSuc($result['info']);
        }elseif($result['status'] === false){
            $this->apiReturnErr($result['info']);
        }

        $entity = array(
            'type'=> UcenterMemberModel::ACCOUNT_TYPE_USERNAME,
            'username' => 'wx_'.md5($openid),
            'password' => $password,
            'from' => OAuth2TypeModel::WEIXIN,
            'mobile' => '',
            'realname' => '',
            'nickname' => '微信用户'.$nickname ,
            'email' => '',
            'idnumber' => '',
            'birthday' => time(),
            'idcode'=>$idcode,
            'head'=>$head,
            //'weixin_bind'=>1, //绑定微信
            'wxopenid'=>$openid,
        );

        $result = apiCall(AccountApi::REGISTER, array($entity));

        if ($result['status']) {
            $uid = $result['info'];
            if($uid > 0){
                $result = apiCall(AccountApi::GET_INFO,array($uid));
                if($result['status']){
                    action_log(BoyeActionConstModel::API_USER_LOGIN, "member", $uid, $uid);
                    $this->apiReturnSuc($result['info']);
                }
            }
        }

        $this->apiReturnErr($result['info']);


    }

}